#!/bin/bash

echo "========================================"
echo "AI Agent Platform SQLite 数据库初始化"
echo "========================================"

echo ""
echo "正在初始化 SQLite 数据库..."
echo ""

# 检查 SQLite 是否已安装
if ! command -v sqlite3 &> /dev/null; then
    echo "错误: 未找到 SQLite3 命令行工具"
    echo "请先安装 SQLite3:"
    echo "  Ubuntu/Debian: sudo apt-get install sqlite3"
    echo "  CentOS/RHEL: sudo yum install sqlite3"
    echo "  macOS: brew install sqlite"
    exit 1
fi

# 检查数据库初始化脚本是否存在
if [ ! -f "../scripts/init-databases.sql" ]; then
    echo "错误: 未找到数据库初始化脚本"
    echo "期望路径: ../scripts/init-databases.sql"
    exit 1
fi

# 创建数据库目录（如果不存在）
mkdir -p db

echo "正在创建数据库文件..."
cd db

# 执行数据库初始化脚本
sqlite3 AgentPlatform.db < ../scripts/init-databases.sql

if [ $? -ne 0 ]; then
    echo "错误: 数据库初始化失败"
    exit 1
fi

echo ""
echo "数据库初始化完成！"
echo ""
echo "数据库文件位置: $(pwd)/AgentPlatform.db"
echo ""
echo "验证数据库内容..."
sqlite3 AgentPlatform.db "SELECT 'SQLite' as DatabaseType, 'AgentPlatform.db' as DatabaseName, COUNT(*) as TableCount FROM sqlite_master WHERE type='table';"
sqlite3 AgentPlatform.db "SELECT * FROM TestConnection;"

echo ""
echo "========================================"
echo "SQLite 数据库初始化成功完成！"
echo "========================================"

exit 0